﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using xxx.Candidates.System.Common.Helpers;
using xxx.Candidates.System.Model.Api;
using xxx.Candidates.System.Model;
using xxx.Candidates.System.Hostd.Extensions;

namespace xxx.Candidates.System.Hostd.Authorization
{
    /// <summary>
    /// 登录验证过滤器
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
    public class AuthorizationAttribute : Attribute, IAuthorizationFilter
    {
        #region 权限标识

        public string Power { get; set; } = "";

        #endregion

        public void OnAuthorization(AuthorizationFilterContext context)
        {
            #region 判断是否登录
            var _tokenManager = context.HttpContext.RequestServices.GetService<TokenManager>();

            if (!_tokenManager.IsAuthenticated())
            {
                ApiResult response = new ApiResult
                {
                    StatusCode = (int)StatusCodeType.Unauthorized,
                    Message = StatusCodeType.Unauthorized.GetEnumText()
                };

                context.Result = new JsonResult(response) { StatusCode = (int)StatusCodeType.Success };
                return;
            }

            #endregion
        }
    }
}
